package graphics;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.LinkedList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.UIDefaults;
import model.Board;
import model.Checker;
import processing.core.*;
import util.Movement;
import util.Position;

/**
 *
 * @author Jah Emanuel
 */
public class BoardDisplay extends PApplet {

    private final float CHECKER_PROPORTION = 0.7f;
    private final int[] COLOR_A = {255, 255, 255};
    private final int[] COLOR_B = {170, 0, 0};
    private final int[] HIGHLIGHT_COLOR = {0, 255, 0};
    private model.Checker[][] board;
    private int size;
    private float tileSize;
    private float checkerSize;
    private double marginH;
    private double marginV;
    private float[] highlight;
    private boolean isHighlight;
    private LinkedList<Position> posHighlight = new LinkedList<Position>();
    private boolean readyToMove;
    Position firstClick;
    Position secondClick;
    AdvancedView view;
    boolean colorOfPlayer;

    public BoardDisplay(model.Checker[][] board, int size, boolean colorOfPlayer) {
        this.board = board;
        this.size = size;
        highlight = new float[2];
        isHighlight = false;
        readyToMove = false;
        this.colorOfPlayer = colorOfPlayer;
    }

    public BoardDisplay(int size, boolean colorOfPLayer) {
        Board bd = new Board();
        this.board = bd.getPositions();
        this.size = size;
        highlight = new float[2];
        isHighlight = false;
        readyToMove = false;
        this.colorOfPlayer = colorOfPLayer;

    }

    public void setBoard(Checker[][] board) {
        this.board = board;
    }

    @Override
    public void setup() {
        int titleBarHt = (int) getBounds().y;
        size = size - titleBarHt;
        tileSize = size / 8;
        checkerSize = tileSize * CHECKER_PROPORTION;
        size(size, size);
        noLoop();
    }

    @Override
    public void draw() {
        boolean isColorA = true;
        float posCheckerI;
        float posCheckerJ;
        for (int i = 0; i < 8; i++) {
            for (int j = 0; j < 8; j++) {
                stroke(255);
                strokeWeight((float) 0.05 * tileSize);
                if (isColorA) {
                    fill(COLOR_A[0], COLOR_A[1], COLOR_A[2]);
                } else {
                    fill(COLOR_B[0], COLOR_B[1], COLOR_B[2]);
                }
                rect(j * tileSize, i * tileSize, tileSize, tileSize);
                isColorA = !isColorA;
                posCheckerI = j * tileSize + tileSize * (1 - CHECKER_PROPORTION) / 2;
                posCheckerJ = i * tileSize + tileSize * (1 - CHECKER_PROPORTION) / 2;
                if (board[i][j] == model.Checker.BLACK) {
                    new CheckerDisplay(this, checkerSize, posCheckerI, posCheckerJ, true, false);
                } else if (board[i][j] == model.Checker.RED) {
                    new CheckerDisplay(this, checkerSize, posCheckerI, posCheckerJ, false, false);
                } else if (board[i][j] == model.Checker.BLACKKING) {
                    new CheckerDisplay(this, checkerSize, posCheckerI, posCheckerJ, true, true);
                } else if (board[i][j] == model.Checker.REDKING) {
                    new CheckerDisplay(this, checkerSize, posCheckerI, posCheckerJ, false, true);
                }
                //System.out.print(board[i][j] + " ");
            }
            //System.out.println("");
            isColorA = !isColorA;
        }

        drawHighlight();
    }

    public void drawHighlight() {
        if (isHighlight) {
            for (Position posToHighlight : posHighlight) {
                noStroke();
                highlight[0] = (float) (marginH + posToHighlight.j * tileSize);
                highlight[1] = (float) (marginV + posToHighlight.i * tileSize);
                fill(HIGHLIGHT_COLOR[0], HIGHLIGHT_COLOR[1], HIGHLIGHT_COLOR[2]);
                rect(highlight[0], highlight[1], tileSize, tileSize);
                if ((posToHighlight.i + posToHighlight.j) % 2 == 0) {
                    fill(COLOR_A[0], COLOR_A[1], COLOR_A[2]);
                } else {
                    fill(COLOR_B[0], COLOR_B[1], COLOR_B[2]);
                }
                float posSpaceI = (float) (highlight[0] + (0.05 * tileSize));
                float posSpaceJ = (float) (highlight[1] + (0.05 * tileSize));
                rect(posSpaceI, posSpaceJ, (float) (tileSize * 0.9), (float) (tileSize * 0.9));
                float posCheckerI = highlight[0] + tileSize * (1 - CHECKER_PROPORTION) / 2;
                float posCheckerJ = highlight[1] + tileSize * (1 - CHECKER_PROPORTION) / 2;
                Checker checkerAtHigh = board[posToHighlight.i][posToHighlight.j];
                boolean drawBlack = (checkerAtHigh == model.Checker.BLACK) || (checkerAtHigh == model.Checker.BLACKKING);
                boolean drawKing = (checkerAtHigh == model.Checker.REDKING) || (checkerAtHigh == model.Checker.BLACKKING);
                if (checkerAtHigh != Checker.EMPTY) {
                    new CheckerDisplay(this, checkerSize, posCheckerI, posCheckerJ, drawBlack, drawKing);
                }

            }

        }
    }

    @Override
    public void mouseClicked() {
        Position posToHighlight = returnPosFromClick(mouseX, mouseY);
        posHighlight.add(posToHighlight);
        isHighlight = true;
        view.selectedPosition(posToHighlight);
        redraw();

    }

    public void addViewer(AdvancedView view) {
        this.view = view;
    }

    public Position returnPosFromClick(int mX, int mY) {
        Position pos = new Position();
        //Columna
        pos.j = (int) ((mX - marginH) / tileSize);
        //Fila
        pos.i = (int) ((mY - marginV) / tileSize);

        return pos;
    }

    public void move(Movement mov) {
        LinkedList<Position> posList=mov.getListOfPositions();
        for (int i = 0; i < posList.size()-1; i++) {
            move(posList.get(i),posList.get(i+1));
        }
    }

    private void move(Position iniPos, Position finalPos) {
        Checker checkerType=board[iniPos.i][iniPos.j];
        board[iniPos.i][iniPos.j]=Checker.EMPTY;
        redraw();
//        int diffPosJ=iniPos.i>finalPos.i?-1:1;
//        int diffPosI=iniPos.j>finalPos.j?-1:1;
//        double delta=0.1*tileSize;
//        float iniPosCheckerI = iniPos.j * tileSize + tileSize * (1 - CHECKER_PROPORTION) / 2;
//        float iniPosCheckerJ = iniPos.i * tileSize + tileSize * (1 - CHECKER_PROPORTION) / 2;
//        float finPosCheckerI = finalPos.j * tileSize + tileSize * (1 - CHECKER_PROPORTION) / 2;
//        float finPosCheckerJ = finalPos.i * tileSize + tileSize * (1 - CHECKER_PROPORTION) / 2;
//        while(true){
//            System.out.println("aqui ando");
//            iniPosCheckerI+=(diffPosI*delta);
//            iniPosCheckerJ+=(diffPosJ*delta);
//            if (checkerType == model.Checker.BLACK) {
//                new CheckerDisplay(this, checkerSize, iniPosCheckerI, iniPosCheckerJ, true, false);
//            } else if (checkerType == model.Checker.RED) {
//                new CheckerDisplay(this, checkerSize, iniPosCheckerI, iniPosCheckerJ, false, false);
//            } else if (checkerType == model.Checker.BLACKKING) {
//                new CheckerDisplay(this, checkerSize, iniPosCheckerI, iniPosCheckerJ, true, true);
//            } else if (checkerType == model.Checker.REDKING) {
//                new CheckerDisplay(this, checkerSize, iniPosCheckerI, iniPosCheckerJ, false, true);
//            }
////            try {
////                Thread.sleep(100);
////            } catch (InterruptedException ex) {
////                Logger.getLogger(BoardDisplay.class.getName()).log(Level.SEVERE, null, ex);
////            }
//            redraw();
//        }
    }

    public void setView(AdvancedView view) {
        this.view = view;
    }

    public void clearHighlight() {
        posHighlight.clear();
        isHighlight = false;
        redraw();
    }
}
